package cn.stylefeng.guns.modular.controller;


import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjUserDynamic;
import cn.stylefeng.guns.modular.service.IIdjUserDynamicService;
import cn.stylefeng.guns.modular.util.HttpClientUtil;
import cn.stylefeng.guns.modular.util.HttpSend;
import cn.stylefeng.guns.modular.util.MessageConstant;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;

/**
 * <p>
 * 用户动态 前端控制器
 * </p>
 *
 * @author TANGWJ
 * @since 2020-07-07
 */
@Controller
@RequestMapping("/modular/user_dynamic")
public class IdjUserDynamicController {

	private static String PREFIX = "/user-dynamic/";

	@Autowired
	private IIdjUserDynamicService service;

	/**
	 * 跳转到用户的首页
	 */
	@RequestMapping
	public String index() {
		return PREFIX + "user_dynamic.html";
	}

	@RequestMapping("details/{id}")
	public ModelAndView add(@PathVariable Long id) {
		ModelAndView index = service.edit(id);
		index.setViewName(PREFIX + "user_dynamic_details.html");
		return index;
	}

	/**
	 * 查询注册用户列表
	 */
	@ResponseBody
	@GetMapping("list")
	public LayuiPageInfo list(String timeLimit, String keyword, Integer indexShow, Integer isAudit) {
		// 获取分页参数
		Page<IdjUserDynamic> page = LayuiPageFactory.defaultPage();

		// 拼接查询条件
		String beginTime = null;
		String endTime = null;

		if (ToolUtil.isNotEmpty(timeLimit)) {
			String[] split = timeLimit.split(" - ");
			beginTime = split[0];
			endTime = split[1];
		}
		// 根据条件查询日志
		return service.getList(page, keyword, beginTime, endTime, indexShow, isAudit);
	}

	/**
	 * 查询注册用户列表
	 */
	@ResponseBody
	@GetMapping("lists")
	public LayuiPageInfo lists(String timeLimit, String keyword, Integer indexShow, Integer isAudit) {
		// 获取分页参数
		Page<IdjUserDynamic> page = LayuiPageFactory.defaultPage();

		// 拼接查询条件
		String beginTime = null;
		String endTime = null;
		if (ToolUtil.isNotEmpty(timeLimit)) {
			String[] split = timeLimit.split(" - ");
			beginTime = split[0];
			endTime = split[1];
		}
		if(indexShow == null){
			indexShow = 1;
		}
		// 根据条件查询日志
		return service.getList(page, keyword, beginTime, endTime, indexShow, isAudit);
	}

	/**
	 * 审核
	 *
	 * @author fengshuonan
	 * @Date 2018/12/24 22:44
	 */
	@RequestMapping("/update")
	@ResponseBody
	public ResponseData update(@RequestBody IdjUserDynamic dynamic) {
		if (ToolUtil.isEmpty(dynamic.getId())) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		if(ToolUtil.isNotEmpty(dynamic.getIsAudit())){
			dynamic.setOperationUserId( LoginContextHolder.getContext().getUserId());
			dynamic.setOperationTime(new Date());
		}
		if(ToolUtil.isNotEmpty(dynamic.getIndexShow())){
			dynamic.setInstallUserId(LoginContextHolder.getContext().getUserId());
			dynamic.setInstallTime(new Date());
		}
		service.setIsAudit(dynamic);
		if(ToolUtil.isNotEmpty(dynamic.getPushId()) && !dynamic.getPushId().equals("undefined")){
			if(dynamic.getIndexShow()== 1){
				HttpSend.send(dynamic.getPushId(), MessageConstant.TYPE_10, MessageConstant.TITLE_10);
			}
		}
		return ResponseData.success();
	}
	@RequestMapping("/delete")
	@ResponseBody
	public ResponseData delete(String id) {
		if (ToolUtil.isEmpty(id)) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		service.delete(Long.valueOf(id));
		return ResponseData.success();
	}
}

